package com.cn.ycy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * -Dlog4j.configurationFile=/Users/ycy/Desktop/test/log4j2.xml
 */
@SpringBootApplication
//@ServletComponentScan
public class Log4SpringbootApplication {

  public static void main(String[] args) {
//    set();
//    SpringApplication.run(Log4SpringbootApplication.class, args);



  }




  static Map<String, String> systems = new LinkedHashMap<>();

  static {
//    systems.put("ch.qos.logback.core.Appender", "E:\\data\\logs\\spring-logback.xml");
//    systems.put("org.apache.logging.log4j.core.impl.Log4jContextFactory", "E:\\data\\logs\\log4j2.xml");
  }

  public static void set() {
    //环境变量可以跳过加载 rdfalog
    String getenv = System.getenv("SKIP.LOAD.RDFA.LOGFILE");
    if (getenv != null) return;
    System.out.println("load log file from rdfa log");
    //设置一个系统变量 logging.config 作为日志配置文件
    for (Map.Entry<String, String> logFileMap : systems.entrySet()) {
      boolean present = isPresent(logFileMap.getKey());
      if (present) {
        String value = logFileMap.getValue();
        File file = new File(value);
        boolean exists = file.exists();
        if(!exists){

          break;
        }
        System.setProperty("logging.config", value);
        System.out.println("set 'logging.config' " + logFileMap.getValue());
        break;
      }
    }
  }

  public static boolean isPresent(String className) {
    try {
      Class.forName(className);
      return true;
    } catch (IllegalAccessError err) {
      return false;
    } catch (Throwable ex) {
      return false;
    }
  }


}
